package com.gxan.web.servlet.pay.standard;

import com.alibaba.fastjson.JSONObject;
import com.gxan.common.LayuiRepResult;
import com.gxan.pojo.SalStandard;
import com.gxan.service.StandardService;
import com.gxan.service.impl.StandardServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/standard/*")
public class StandardServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取URI地址
        System.out.println(request.getRequestURI());
        String uri = request.getRequestURI();
        //走不同的方法
        switch (uri){
            //查询
            case "/standard/salFindAll":
                salFindAll(request,response);
                break;
            //添加
            case "/standard/salAdd":
                salAdd(request,response);
                break;
            //删除
            case "/standard/salDel":
                salDel(request,response);
                break;
            //复核
            case "/standard/salReview":
                salReview(request,response);
                break;
            //变更
            case "/standard/salChange":
                salChange(request,response);
                break;
            //明细
            case "/standard/salDetails":
                salDetails(request,response);
                break;
            //修改
            case "/standard/salEdit":
                salEdit(request,response);
                break;
            //搜索
            case "/standard/search":
                salSearch(request,response);
                break;
            default:
                break;
        }
    }


    //定义为成员变量 --线程不安全 --如果此页面并非量高情况不适合
    private StandardService standardService =  new StandardServiceImpl();
    // 自定义成员变量 Map的Java对象
    Map resultMap = new HashMap<>();
    /**
     * 查询所有
     */
    public void salFindAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        LayuiRepResult result = standardService.salFindAll(page,limit);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSONObject.toJSONString(result));
        response.getWriter().flush();
        response.getWriter().close();

    }

    /**
     * 添加
     */
    public void salAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //添加成功？返回json数据回去
        response.setContentType("application/json;charset=utf-8");
        try{
            // 如果客户端传递的是json字符串格式，那么不能用request.getParamter("name");
            String params = request.getReader().readLine();
        // 将json字符串转换为Java对象时，我们需要用第三方fastjson(alibaba)依赖
        SalStandard salStandard = JSONObject.parseObject(params, SalStandard.class);
        int result = standardService.addStandard(salStandard);
            if(result > 0){
                resultMap.put("code",0);
                resultMap.put("msg","添加成功！");
                // 需要将Java对象转换为json字符串对象才能在网络中进行传输
                response.getWriter().write(JSONObject.toJSONString(resultMap));
            }
        }catch (Exception e){
        e.printStackTrace();
        resultMap.put("code",-1);
        resultMap.put("msg","系统开小差了，请联系管理员");
        // 需要将Java对象转换为json字符串对象才能在网络中进行传输
        response.getWriter().write(JSONObject.toJSONString(resultMap));
    }
        response.getWriter().flush();
        response.getWriter().close();
    }
    /**
     * 删除
     */
    public void salDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            String sal_standard_code = request.getParameter("sal_standard_code");
            int result = standardService.deleteStandard(sal_standard_code);
            if (result > 0) {
                resultMap.put("code", 0);
                resultMap.put("msg", "修改成功！");
                // 需要将Java对象转换为json字符串对象才能在网络中进行传输
                response.getWriter().write(JSONObject.toJSONString(resultMap));
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("msg", "系统开小差了，请联系管理员");
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            response.getWriter().write(JSONObject.toJSONString(resultMap));
        }
        response.getWriter().flush();
        response.getWriter().close();
    }
    /**
     * 修改
     */
    public void salEdit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            // 如果客户端传递的是json字符串格式，那么不能用request.getParamter("name");
            String params = request.getReader().readLine();
            // 将json字符串转换为Java对象时，我们需要用第三方fastjson(alibaba)依赖
            SalStandard salStandard = JSONObject.parseObject(params, SalStandard.class);
            int result = standardService.updateStandard(salStandard);
            if (result > 0) {
                resultMap.put("code", 0);
                resultMap.put("msg", "修改成功！");
                // 需要将Java对象转换为json字符串对象才能在网络中进行传输
                response.getWriter().write(JSONObject.toJSONString(resultMap));
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("msg", "系统开小差了，请联系管理员");
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            response.getWriter().write(JSONObject.toJSONString(resultMap));
        }
        response.getWriter().flush();
        response.getWriter().close();
    }

    /**
     * 复核
     */
    public void salReview(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            // 如果客户端传递的是json字符串格式，那么不能用request.getParamter("name");
            String params = request.getReader().readLine();
            // 将json字符串转换为Java对象时，我们需要用第三方fastjson(alibaba)依赖
            SalStandard salStandard = JSONObject.parseObject(params, SalStandard.class);
            int result = standardService.reviewStandard(salStandard);
            if (result > 0) {
                resultMap.put("code", 0);
                resultMap.put("msg", "修改成功！");
                // 需要将Java对象转换为json字符串对象才能在网络中进行传输
                response.getWriter().write(JSONObject.toJSONString(resultMap));
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("msg", "系统开小差了，请联系管理员");
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            response.getWriter().write(JSONObject.toJSONString(resultMap));
        }
        response.getWriter().flush();
        response.getWriter().close();
    }
    /**
     * 变更
     */
    public void salChange(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            // 如果客户端传递的是json字符串格式，那么不能用request.getParamter("name");
            String params = request.getReader().readLine();
            // 将json字符串转换为Java对象时，我们需要用第三方fastjson(alibaba)依赖
            SalStandard salStandard = JSONObject.parseObject(params, SalStandard.class);
            int result = standardService.changeStandard(salStandard);
            if (result > 0) {
                resultMap.put("code", 0);
                resultMap.put("msg", "变更成功！");
                // 需要将Java对象转换为json字符串对象才能在网络中进行传输
                response.getWriter().write(JSONObject.toJSONString(resultMap));
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("msg", "系统开小差了，请联系管理员");
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            response.getWriter().write(JSONObject.toJSONString(resultMap));
        }
        response.getWriter().flush();
        response.getWriter().close();
    }
    /**
     * 明细
     */
    public void salDetails(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            // 如果客户端传递的是json字符串格式，那么不能用request.getParamter("name");
            String params = request.getReader().readLine();
            // 将json字符串转换为Java对象时，我们需要用第三方fastjson(alibaba)依赖
            SalStandard salStandard = JSONObject.parseObject(params, SalStandard.class);
            int result = standardService.detailsStandard(salStandard);
            if (result > 0) {
                resultMap.put("code", 0);
                resultMap.put("msg", "！");
                // 需要将Java对象转换为json字符串对象才能在网络中进行传输
                response.getWriter().write(JSONObject.toJSONString(resultMap));
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("msg", "系统开小差了，请联系管理员");
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            response.getWriter().write(JSONObject.toJSONString(resultMap));
        }
        response.getWriter().flush();
        response.getWriter().close();
    }

    /**
     * 搜索
     * @param request
     * @param response
     */
    private void salSearch(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try{
            String sal_standard_name = request.getParameter("sal_standard_name");
            Integer review_status = Integer.parseInt(request.getParameter("review_status"));
            Integer page =  Integer.parseInt(request.getParameter("page"));
            Integer limit =  Integer.parseInt(request.getParameter("limit"));
            List<SalStandard> salStandards = standardService.searchStandard(page,limit,sal_standard_name,review_status);
            resultMap.put("code",0);
            resultMap.put("msg","success");
            resultMap.put("count",standardService.countSearchStandard(sal_standard_name,review_status));
            resultMap.put("data",salStandards);
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            response.getWriter().write(JSONObject.toJSONString(resultMap));
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("code",-1);
            resultMap.put("msg","系统开小差了，请联系管理员");
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            response.getWriter().write(JSONObject.toJSONString(resultMap));
        }
        response.getWriter().flush();
        response.getWriter().close();
    }
}
